package com.huixuebao.after.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huixuebao.after.dto.SettleDTO;
import com.huixuebao.after.dto.SettleToExamineDTO;
import com.huixuebao.after.entity.PolicyOrderEntity;
import com.huixuebao.after.mapper.PolicyMapper;
import com.huixuebao.after.service.SettleService;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SettleServiceImpl implements SettleService {

    @Autowired
    PolicyMapper policyMapper;

    /**
     * 获取保单审核列表信息
     * @param current
     * @param size
     * @param merchantsName
     * @param courseName
     * @param phone
     * @return
     */
    @Override
    public ApiResult getSettleToExamine(Integer current, Integer size, String merchantsName, String courseName, String phone,Integer settleState) {
        ApiResult apiResult = new ApiResult();
        try{
            if(ObjectUtil.isNotNull(current) && ObjectUtil.isNotNull(size)){
                IPage<PolicyOrderEntity> page = new Page<>(current,size);
                Map<String,Object> map = new HashMap<>();
                map.put("merchantsName",merchantsName);
                map.put("courseName",courseName);
                map.put("phone",phone);
                map.put("settleState",String.valueOf(settleState));
                map.put("orderByField","create_time");
                map.put("orderBySe","asc");
                IPage<SettleToExamineDTO> dtoPage = policyMapper.selectSettle(page,map);
                apiResult.setData(dtoPage);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("查询保单审核列表信息成功!!");
            }else {
                Map<String,Object> maps = new HashMap<>();
                maps.put("courseName",courseName);
                maps.put("merchantsName",merchantsName);
                maps.put("phone",phone);
                maps.put("settleState",String.valueOf(settleState));
                maps.put("orderByField","create_time");
                maps.put("orderBySe","asc");
                List<SettleToExamineDTO> dtoList = policyMapper.selectSettleList(maps);
                apiResult.setData(dtoList);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("查询保单审核列表信息成功!!");
            }
        }catch (Exception e){
            log.info("查询保单列表失败!!!" + e.getMessage());
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("查询保单列表信息失败!!");
        }
        return apiResult;
    }

    /**
     * 设置理赔额度
     * @param settleDTO
     * @return
     */
    @Override
    public ApiResult setSettleLimit(SettleDTO settleDTO) {
        ApiResult apiResult = new ApiResult();
        try {
           String orderId = settleDTO.getId();
           if(StrUtil.isNotEmpty(orderId)){
               PolicyOrderEntity orders = policyMapper.selectOne(new LambdaQueryWrapper<PolicyOrderEntity>().eq(PolicyOrderEntity::getId,orderId));
               orders.setSettleState(2);
               policyMapper.updateById(orders);
               apiResult.setSuccess(true);
               apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
               apiResult.setMessage("设置理赔额度成功!!");
           }else {
               apiResult.setSuccess(false);
               apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
               apiResult.setMessage("当前订单为空!!");
           }
        }catch (Exception e){
            log.info("设置理赔额度异常!!");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("当前订单为空!!" + e.getMessage());
        }
        return apiResult;
    }

    /**
     * 理赔驳回内容
     * @param settleDTO
     * @return
     */
    @Override
    public ApiResult submitSettleReject(SettleDTO settleDTO) {
        ApiResult apiResult = new ApiResult();
        try {
            String orderId = settleDTO.getId();
            String content = settleDTO.getRejectContent();
            if (StrUtil.isNotEmpty(orderId) && StrUtil.isNotEmpty(content)) {

                PolicyOrderEntity id = policyMapper.selectOne(new LambdaQueryWrapper<PolicyOrderEntity>().eq(PolicyOrderEntity::getId, orderId));
                id.setRejectContent(content);
                id.setSettleState(5);
                policyMapper.updateById(id);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("提交驳回信息成功!!");
            } else {
                apiResult.setSuccess(false);
                apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
                apiResult.setMessage("当前订单为空!!");
            }
        } catch (Exception e) {
            log.info("提交驳回信息异常!!");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("提交驳回信息失败!!" + e.getMessage());
        }
        return apiResult;
    }
}
